package com.oig.sys.auth.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbResourceDao;
import com.oig.sys.auth.dao.ITbRoleResourceDao;
import com.oig.sys.auth.dto.ResourceDto;
import com.oig.sys.auth.entity.TbRoleResource;
import com.oig.sys.auth.service.ITbRoleResourceService;
import com.oig.sys.auth.vo.ResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbRoleResourceServiceImpl implements ITbRoleResourceService {

	private final ITbRoleResourceDao tbRoleResourceDao;
	private final ITbResourceDao tbResourceDao ;

	@Autowired
	public TbRoleResourceServiceImpl(ITbRoleResourceDao tbRoleResourceDao,ITbResourceDao tbResourceDao){
		 this.tbRoleResourceDao = tbRoleResourceDao;
		 this.tbResourceDao = tbResourceDao;
	}


	@Override
	public List<ResourceVo> findResourceByRole(Integer roleId) {
		List<Integer> resourceIdList = this.tbRoleResourceDao.findAllResourceIdByRole(roleId);
		return this.tbResourceDao.findSystemMenuByIdList(resourceIdList);
	}

	@Override
	public List<ResourceVo> findOkResourceByRole(List<Integer> roleIdList, Integer systemId) {
		List<Integer> resourceIdList = this.tbRoleResourceDao.findAllResourceIdByRole(roleIdList, systemId, CommonEnum.CommonStatusEnum.OK.getCode()) ;
		return this.tbResourceDao.findSystemMenuByIdList(resourceIdList);
	}

	@Transactional
	@Override
	public List<ResourceVo> addRoleResource(Integer roleId, List<ResourceDto> resourceDtoList) {
		List<Integer> resourceIdList = new ArrayList<>();
		for (ResourceDto resourceDto : resourceDtoList){
			if (resourceDto.getId()==null){
				throw new BadRequestException("菜单id为空");
			}
			TbRoleResource roleResource = new TbRoleResource();
			roleResource.setRoleId(roleId);
			roleResource.setResourceId(resourceDto.getId());
			this.tbRoleResourceDao.save(roleResource) ;
			resourceIdList.add(roleResource.getResourceId()) ;
		}
		return tbResourceDao.findSystemMenuByIdList(resourceIdList);
	}

	@Transactional
	@Override
	public void addRoleResourceId(Integer roleId, List<Integer> resourceIdList) {
		for (Integer resourceId : resourceIdList) {
			TbRoleResource roleResource = new TbRoleResource();
			roleResource.setRoleId(roleId);
			roleResource.setResourceId(resourceId);
			this.tbRoleResourceDao.save(roleResource);
		}
	}

	@Transactional
	@Override
	public void delRoleResourceId(Integer roleId, List<Integer> resourceIdList) {
		List<Integer> delIdList = this.tbRoleResourceDao.findAllDelId(roleId, resourceIdList);
		if (delIdList!=null && !delIdList.isEmpty()) {
			this.tbRoleResourceDao.deleteByIdList(delIdList);
		}
	}

	@Transactional
	@Override
	public List<ResourceVo> updateRoleResource(Integer roleId, List<ResourceDto> resourceDtoList) {
		List<Integer> resourceIdList = new ArrayList<>();
		List<Integer> roleResourceIdList = new ArrayList<>() ;
		for (ResourceDto resourceDto : resourceDtoList) {
			if (resourceDto.getId()==null){
				throw new BadRequestException("菜单id为空");
			}
			TbRoleResource roleResource = this.tbRoleResourceDao.getByRoleIdAndResourceId(roleId, resourceDto.getId()) ;
			if (roleResource==null){
				roleResource = new TbRoleResource();
				roleResource.setRoleId(roleId);
				roleResource.setResourceId(resourceDto.getId());
				this.tbRoleResourceDao.save(roleResource) ;
			}
			roleResourceIdList.add(roleResource.getId()) ;
			resourceIdList.add(roleResource.getResourceId()) ;
		}
		List<Integer> delIdList = this.tbRoleResourceDao.findAllDelId(roleId, roleResourceIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleResourceDao.deleteByIdList(delIdList) ;
		}
		return tbResourceDao.findSystemMenuByIdList(resourceIdList);
	}

	@Transactional
	@Override
	public void delRoleResourceByRoleId(Integer roleId) {
		List<Integer> delIdList = this.tbRoleResourceDao.findAllIdByRoleId(roleId);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleResourceDao.deleteByIdList(delIdList) ;
		}
	}

	@Transactional
	@Override
	public void delRoleResourceByResourceId(Integer resourceId) {
		List<Integer> delIdList = this.tbRoleResourceDao.findAllIdByResourceId(resourceId);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleResourceDao.deleteByIdList(delIdList);
		}
	}

	@Override
	public List<Integer> findRoleIdByResourceId(Integer resourceId) {
		return this.tbRoleResourceDao.findAllRoleByResourceId(resourceId);	//role是直接删除的，所以查的都是生效的
	}
}
